﻿using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Panda.DynamicWebApi;
using System.Reflection;

namespace Framework.DynamicApiController
{
	public class ServiceActionRouteFactory : IActionRouteFactory
	{
		public string CreateActionRouteModel(string areaName, string controllerName, ActionModel action)
		{
			var ApiPreFix = AppConsts.DefaultApiPreFix ?? "api";    // 读取API路由前缀,没设置默认为api
			

			var controllerType = action.ActionMethod.DeclaringType;
			var serviceAttribute = controllerType.GetCustomAttribute<ServiceAttribute>();

			var _controllerName = controllerName;
			if(serviceAttribute.ServiceName != string.Empty)
			{
				_controllerName = serviceAttribute.ServiceName;
			}

			return $"{ApiPreFix}/{_controllerName}/{action.ActionName.Replace("Async", "")}";
		}
	}
}
